package com.globallogic.javatrainings.sergiiivanov.controller;

import com.globallogic.javatrainings.sergiiivanov.model.Const;
import com.globallogic.javatrainings.sergiiivanov.model.University;
import com.globallogic.javatrainings.sergiiivanov.resources.Messages;
import com.globallogic.javatrainings.sergiiivanov.view.MainMenuPage;

class MainMenuController extends BasicController {

    private enum states {
        SELECTED, STARTING, WAITING
    };

    private MainMenuPage page = new MainMenuPage();
    private int command;
    private states currentState = states.STARTING;
    String[] menuItems = {
            Messages.MAIN_MENU_EXIT, // 0
            Messages.MAIN_MENU_STUDENTS, // 1
            Messages.MAIN_MENU_COURSES, // 2
            Messages.MAIN_MENU_TEACHERS, // 3
            Messages.MAIN_MENU_PERSONNEL, // 4
            Messages.MAIN_MENU_BUILDINGS, // 5
            "Save as ..." // 6
    };

    @Override
    BasicController initiate(University university) {
        if (currentState == states.STARTING) {
            return showMainMenu();
        }
        if (currentState == states.WAITING) {
            return readCommand();
        }
        if (currentState == states.SELECTED) {
            return commandResponse();
        }
        return this;
    }

    private BasicController commandResponse() {
        switch (command) {
        case 1:
            currentState = states.STARTING;
            return new StudentsMenuController();
        case 2:
            currentState = states.STARTING;
            return new CoursesMenuController();
        case 3:
            currentState = states.STARTING;
            return new TeachersMenuController();
        case 4:
            currentState = states.STARTING;
            return new PersonnelDetailsController();
        case 5:
            currentState = states.STARTING;
            return new BuildingsMenuController();
        case 6:
            currentState = states.STARTING;
            return changeFileName();
        case 0:
            return null;
        default:
            break;
        }
        currentState = states.WAITING;
        return this;
    }

    private BasicController changeFileName() {
        page.show("Enter new file name");
        String newFileName = InputController.readString().trim();
        if (!Const.STRING_EMPTY.equals(newFileName)) {
            if (!newFileName.endsWith(".usav")) {
                newFileName = newFileName + ".usav";
            }
            Main.dr.fileName = newFileName;
            return null;
        }
        return this;
    }

    private BasicController readCommand() {
        command = InputController.readInt();
        currentState = states.SELECTED;
        return this;
    }

    private BasicController showMainMenu() {
        page.showMainMenu(
                Main.dr.fileName,
                Messages.UNIVERSITY_MAIN_MENU_BREADCRUMP,
                menuItems,
                Messages.SELECT_ACTION_AND_PRESS_ENTER
                );
        currentState = states.WAITING;
        return this;
    }

}
